#include <stdio.h>
#include <stdlib.h>
#include <string.h>

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    int mid_index = (nums1Size + nums2Size) / 2;
    double mid_value = 0.0f;
    int index1 = 0;
    int index2 = 0;
    int temp = 0;

    printf("nums1Size = %d, nums2Size = %d, mid_index: %d\r\n", nums1Size, nums2Size, mid_index);
    if(mid_index == 0) {
        if(nums1Size) {
            mid_value = nums1[0];
        } else{
            mid_value = nums2[0];
        }
    } else {
        for(int i = 0; i < mid_index + 1; i++) {
            if((nums1Size + nums2Size) % 2) {
                if((index1 < nums1Size) && (index2 < nums2Size)) {
                    if(nums1[index1] <= nums2[index2]) {
                        temp = nums1[index1];
                        index1++;
                    } else {
                        temp = nums2[index2];
                        index2++;
                    }

                    if((index1 + index2) > mid_index) {
                        mid_value = (double)temp;
                        break;
                    }
                } else if(index1 < nums1Size) {
                    if((index1 + index2) == mid_index) {
                        mid_value = (double)nums1[index1];
                        index1++;
                        break;
                    } else {
                        index1++;
                    }
                } else if(index2 < nums2Size) {
                    if((index1 + index2) == (mid_index)) {
                        mid_value = (double)nums2[index2];
                        index2++;
                        break;
                    } else {
                        index2++;
                    }
                }
            } else {
                if((index1 < nums1Size) && (index2 < nums2Size)) {

                    if(nums1[index1] <= nums2[index2]) {
                        temp = nums1[index1];
                        if(((index1 + index2) == mid_index) || ((index1 + index2) == (mid_index - 1))) {
                            mid_value += (double)temp / 2;
                        }
                        index1++;
                    } else {
                        temp = nums2[index2];
                        if(((index1 + index2) == mid_index) || ((index1 + index2) == (mid_index - 1))) {
                            mid_value += (double)temp / 2;
                        }
                        index2++;
                    }

                } else if(index1 < nums1Size) {
                    if(((index1 + index2) == mid_index) || ((index1 + index2) == (mid_index - 1))) {
                        mid_value += (double)nums1[index1] / 2;
                        index1++;
                    } else {
                        index1++;
                    }
                } else if(index2 < nums2Size) {
                    if(((index1 + index2) == mid_index) || ((index1 + index2) == (mid_index - 1))) {
                        mid_value += (double)nums2[index2] / 2;
                        index2++;
                    } else {
                        index2++;
                    }
                }
            }
        }
    }

    printf("\r\nmedian: %f\r\n", mid_value);
    return mid_value;
}

int main(void)
{
    // int nums1[] = {1, 3};
    // int nums2[] = {2};

    // int nums1[] = {1, 2};
    // int nums2[] = {3, 4};

    int nums1[] = {0, 1, 3, 5, 7};
    int nums2[] = {2, 4};

    // int nums1[] = {1, 3, 5};
    // int nums2[] = {2, 4, 6};

    // int nums1[0] = {};
    // int nums2[] = {1, 2, 3, 4};

    // int nums1[0] = {};
    // int nums2[] = {1, 2, 3, 4, 5};

    // int nums1[] = {1, 2};
    // int nums2[0] = {};

    // int nums1[0] = {};
    // int nums2[] = {1, 2};

    // int nums1[0] = {};
    // int nums2[] = {1};

    findMedianSortedArrays(nums1, sizeof(nums1) / sizeof(nums2[0]), nums2, sizeof(nums2) / sizeof(nums2[0]));
    return 0;
}
